'use strict';


// Declare app level module which depends on filters, and services
var app = angular.module('app', [
    'ngAnimate',
    'ngCookies',
    'ngStorage',
    'ui.router',
    'ui.bootstrap',
    'ui.load',
    'ui.jq',
    'ui.validate',
    'pascalprecht.translate',
    'ng.ueditor',
    'app.filters',
    'app.services',
    'app.directives',
    'app.controllers'
])
    .run(
    ['$rootScope', '$state', '$stateParams',
        function ($rootScope, $state, $stateParams) {
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;
        }
    ]
    )
    .constant('APP_CONFIG', {
        tokenCookieKey: '_access_token',
        openIdHeaderName: 'X-ACCESS_TOKEN',

        apiHost: 'http://localhost:9080',

        eventUserNotLogin: 'eventUserNotLogin',
        eventUserLoaded: 'eventUserLoaded',
        eventHttpError: 'eventHttpError'
    })
    .config(
    ['$stateProvider', '$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$httpProvider', '$sceDelegateProvider', 'APP_CONFIG',
        function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $httpProvider, $sceDelegateProvider, APP_CONFIG) {

            // lazy controller, directive and service
            app.controller = $controllerProvider.register;
            app.directive = $compileProvider.directive;
            app.filter = $filterProvider.register;
            app.factory = $provide.factory;
            app.service = $provide.service;
            app.constant = $provide.constant;
            app.value = $provide.value;

            $httpProvider.interceptors.push('appInterceptor');

            $sceDelegateProvider.resourceUrlWhitelist([
                'self',
                APP_CONFIG.apiHost + '/**'
            ]);

            $urlRouterProvider
                .otherwise('/access/signin');

            $stateProvider
                .state('app', {
                    abstract: true,
                    url: '/app',
                    templateUrl: 'tpl/app.html',
                    resolve: {
                        userRes: [
                            'accountService', function(accountService) {
                                return accountService.getUserData();
                            }
                        ]
                    }
                })
                .state('app.dashboard', {
                    url: '/dashboard',
                    templateUrl: 'tpl/app-dashboard.html',
                    resolve: {
                        userRes: [
                            'accountService', function(accountService) {
                                return accountService.getUserData();
                            }
                        ]
                    }
                })
                .state('app.profile', {
                    url: '/profile',
                    templateUrl: 'tpl/app-profile.html'
                })
                .state('app.users', {
                    url: '/users',
                    templateUrl: 'tpl/app-users.html'
                })
                .state('app.performance', {
                    url: '/performance',
                    templateUrl: 'tpl/app-performance.html'
                })
                .state('app.performance-edit', {
                    url: '/performance/:id',
                    templateUrl: 'tpl/app-performance-edit.html',
                    resolve: {
                        js: [
                            'uiLoad', function(uiLoad) {
                                return uiLoad.load([
                                    'http://ueditor.baidu.com/ueditor/ueditor.config.js',
                                    'http://apps.bdimg.com/libs/ueditor/1.4.3.1/ueditor.all.js'
                                ]);
                            }
                        ]
                    }
                })
                .state('app.trainning-category', {
                    url: '/trainning-category',
                    templateUrl: 'tpl/app-trainning-category.html'
                })
                .state('app.trainning', {
                    url: '/trainning',
                    templateUrl: 'tpl/app-trainning.html'
                })
                .state('app.trainning-enroll', {
                    url: '/trainning-enroll',
                    templateUrl: 'tpl/app-trainning-enroll.html'
                })
                .state('app.venue', {
                    url: '/venue',
                    templateUrl: 'tpl/app-venue.html'
                })
                .state('app.venue-edit', {
                    url: '/venue-eidt/:id',
                    templateUrl: 'tpl/app-venue-edit.html'
                })
                .state('app.venue-book', {
                    url: '/venue-book/:id',
                    templateUrl: 'tpl/app-venue-book.html',
                    resolve: {
                        fileRes: [
                            'uiLoad', function(uiLoad) {
                                return uiLoad.load(['js/libs/clndr.css', 'js/libs/underscore.js', 'js/libs/moment-with-zh-cn.js', 'js/libs/clndr.js']);
                            }
                        ]
                    }
                })
                .state('app.reports', {
                    url: '/reports',
                    templateUrl: 'tpl/app-reports.html'
                })

                .state('access', {
                    url: '/access',
                    template: '<div ui-view class="fade-in-right-big smooth"></div>',
                    resolve: {
                        tokenRes: [
                            'oauthService', function(oauthService) {
                                return oauthService.getBasicToken();
                            }
                        ]
                    }
                })
                .state('access.signin', {
                    url: '/signin',
                    templateUrl: 'tpl/access-signin.html'
                })
                .state('access.signup', {
                    url: '/signup',
                    templateUrl: 'tpl/access-signup.html'
                })
                .state('access.forgotpwd', {
                    url: '/forgotpwd',
                    templateUrl: 'tpl/access-forgotpwd.html'
                })
                .state('access.404', {
                    url: '/404',
                    templateUrl: 'tpl/access-404.html'
                })
                .state('app.config', {
                    url: '/config',
                    templateUrl: 'tpl/app-config.html',
                    resolve: {
                        js: [
                            'uiLoad', function(uiLoad) {
                                return uiLoad.load([
                                    'http://ueditor.baidu.com/ueditor/ueditor.config.js',
                                    'http://apps.bdimg.com/libs/ueditor/1.4.3.1/ueditor.all.js'
                                ]);
                            }
                        ]
                    }
                });

        }
    ]
    )

    .config(['$translateProvider', function ($translateProvider) {

        // Register a loader for the static files
        // So, the module will search missing translation tables under the specified urls.
        // Those urls are [prefix][langKey][suffix].
        $translateProvider.useStaticFilesLoader({
            prefix: 'l10n/',
            suffix: '.json'
        });

        // Tell the module what language to use by default
        $translateProvider.preferredLanguage('zh_CN');

        // Tell the module to store the language in the local storage
        $translateProvider.useLocalStorage();

    }])

    /**
     * jQuery plugin config use ui-jq directive , config the js and css files that required
     * key: function name of the jQuery plugin
     * value: array of the css js file located
     */
    .constant('JQ_CONFIG', {
        easyPieChart: ['js/jquery/charts/easypiechart/jquery.easy-pie-chart.js'],
        sparkline: ['js/jquery/charts/sparkline/jquery.sparkline.min.js'],
        plot: ['js/jquery/charts/flot/jquery.flot.min.js',
            'js/jquery/charts/flot/jquery.flot.resize.js',
            'js/jquery/charts/flot/jquery.flot.tooltip.min.js',
            'js/jquery/charts/flot/jquery.flot.spline.js',
            'js/jquery/charts/flot/jquery.flot.orderBars.js',
            'js/jquery/charts/flot/jquery.flot.pie.min.js'],
        slimScroll: ['js/jquery/slimscroll/jquery.slimscroll.min.js'],
        sortable: ['js/jquery/sortable/jquery.sortable.js'],
        nestable: ['js/jquery/nestable/jquery.nestable.js',
            'js/jquery/nestable/nestable.css'],
        filestyle: ['js/jquery/file/bootstrap-filestyle.min.js'],
        slider: ['js/jquery/slider/bootstrap-slider.js',
            'js/jquery/slider/slider.css'],
        chosen: ['js/jquery/chosen/chosen.jquery.min.js',
            'js/jquery/chosen/chosen.css'],
        TouchSpin: ['js/jquery/spinner/jquery.bootstrap-touchspin.min.js',
            'js/jquery/spinner/jquery.bootstrap-touchspin.css'],
        wysiwyg: ['js/jquery/wysiwyg/bootstrap-wysiwyg.js',
            'js/jquery/wysiwyg/jquery.hotkeys.js'],
        dataTable: ['js/jquery/datatables/jquery.dataTables.min.js',
            'js/jquery/datatables/dataTables.bootstrap.js',
            'js/jquery/datatables/dataTables.bootstrap.css'],
        vectorMap: ['js/jquery/jvectormap/jquery-jvectormap.min.js',
            'js/jquery/jvectormap/jquery-jvectormap-world-mill-en.js',
            'js/jquery/jvectormap/jquery-jvectormap-us-aea-en.js',
            'js/jquery/jvectormap/jquery-jvectormap.css'],
        footable: ['js/jquery/footable/footable.all.min.js',
            'js/jquery/footable/footable.core.css']
    }
    )


    .constant('MODULE_CONFIG', {
        select2: ['js/jquery/select2/select2.css',
            'js/jquery/select2/select2-bootstrap.css',
            'js/jquery/select2/select2.min.js',
            'js/modules/ui-select2.js']
    }
    )
    ;
